#! @BASH@
# -*- mode: sh -*-
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Written by Victor Ananjevsky, 2010
#

VERSION=@VERSION@
GETTEXT_DOMAIN=@PACKAGE@

function usage () {
    echo $"${0##*/}, version - $VERSION"
    echo $"  Recode tags from specified to system encoding"
    echo $"Usage: ${0##*/} [-hv] -e ENCODING <filename> ..."
    echo $"   -e ENCODING      specify encoding of media tags"
    echo $"   -h               show this help"
    echo $"   -v               show version"
}

# Parse command line
OPTIND=1
while getopts f:hsv opt ; do
    case "$opt" in
        e) ENCODING=$OPTARG ;;
        h) usage ; exit 0 ;;
        v) echo $"${0##*/}, version - $VERSION" ; exit 0 ;;
    esac
done
shift $(($OPTIND - 1))
[[ "$1" == '--' ]] && shift

if [[ -z $ENCODING ]]; then
    echo $"Error: encoding not set"
    exit 1
fi

# main loop
for file 
do
    eval $(mtag --shell "$file")
    new_artist="$(echo $MTAG_ARTIST | iconv -f $ENCODING)"
    new_album="$(echo $MTAG_ALBUM | iconv -f $ENCODING)"
    new_title="$(echo $MTAG_TITLE | iconv -f $ENCODING)"
    new_comment="$(echo $MTAG_COMMENT | iconv -f $ENCODING)"
    mtag -s -u -a "$new_artist" -t "$new_title" -A "$new_album" -c "$new_comment" "$file"
done
